// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'boyi-5g77xpkp0aa0181c'
})

// 云函数入口函数
exports.main = async (event, context) => {

  const wxContext = cloud.getWXContext();
  console.log(wxContext);
  console.log(event);

  // 如果获取失败
  if(wxContext.OPENID == undefined) {
    var result = {};
    result.errCode = 1;
    result.errMsg = "未能正确获取到当前用户的openid，请退出小程序重试";
    console.log("未能正确获取到当前用户的openid，请退出小程序重试");
    var data = {};
    result.data = data;
    return result;
  }

  // 获取成功
  var result = {};
  result.errCode = 0;
  result.errMsg = "获取openid成功";
  result.event = event;
  result.wxContext = wxContext;
  console.log("云函数获取openid成功");
  var data = {
    openId: wxContext.OPENID
  };
  result.data = data;
  return result;
}